<?php


error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once 'constants.php';
require_once 'class_Status.php';




class Forum {
    protected $name;
    protected $startPage;
    protected $currentPage;
    protected $currentPageNumber;
    protected $type;
    protected $topicTitle;
    protected $dom;
    
    protected $status;
    
    function __construct($name, $startPage, $type, $topicTitle) {
        $this->name = $name;
        $this->startPage = $startPage;
        $this->currentPage = $startPage;
        $this->type = $type;
        $this->topicTitleMatches = $topicTitle;
        $this->currentPageNumber = 0;
        
        $this->status = null;
        $this->dom = null;
    }
    
    function getStatus() {
        return $this->status;
    }
    
    function getName() {
        return $this->name;
    }
    
    function matchFoundAnd($forumText, $matches) {
        foreach($matches as $match) {
            if(stripos($forumText, $match) === false) {
                return false;    
            }
        }
        return true;
    }
    
    function matchFoundOr($forumText, $matches) {
        foreach($matches as $match) {
            if(stripos($forumText, $match) !== false) {
                return true;    
            }
        }
        return false;
    }
    
    /**
    @note. The Next-Page feature is not working properly on all forums. Disabled for now.
    
    function goToNextPage() {
        global $FORUMS_CONFIG;
        
        $foundNextPage =  false;
        $link = '';
        $xpath = new DOMXpath($this->dom);
        $elements = $xpath->query($FORUMS_CONFIG[$this->type][NEXT_BUTTON]);
        
        if (!is_null($elements)) {
            foreach ($elements as $element) {
                $link = $element->getAttribute('href');
                $foundNextPage = true;
                break;
            }
        }
         
        if($foundNextPage) {
            $this->currentPage = $link;
        }
        else {
            $this->status = Status::status(Status::CODE_NEXT_NOT_FOUND, Status::DESCR_NEXT_NOT_FOUND, $this->currentPageNumber);
        }
        
        return $foundNextPage;
    }
    */
    
    function go(){
        global $FORUMS_CONFIG;
        
        $this->dom = new DOMDocument();
        
        $foundTopicTitleOnPage = false;
        
        while(!$foundTopicTitleOnPage) {
            $this->currentPageNumber++;
            $page = file_get_contents($this->currentPage);
            @$this->dom->loadHTML($page);

            /**
                @todo. Perform this initial check, it should be faster if there are many pages w/o our topic
            */
            
            $rows = $this->dom->getElementsByTagName('tr');
            $this->currentRowNumber = $FORUMS_CONFIG[$this->type][ROW_OFFSET];
            
            foreach($rows as $row) {
                $this->currentRowNumber++;
                
                if($this->matchFoundAnd($row->nodeValue, $this->topicTitleMatches)) {
                    $foundTopicTitleOnPage = true;
                    $this->status = Status::status(Status::CODE_TOPIC_FOUND, Status::DESCR_TOPIC_FOUND, $this->currentPageNumber, $this->currentRowNumber);
                    
                    $foundRow = $row;
                    
                    $columns = $foundRow->getElementsByTagName('td');
                    $columnIndex = 0;
                    
                    foreach($columns as $column) {
                        if(isset($FORUMS_CONFIG[$this->type][COLUMN_CONFIG][$columnIndex])) {
                            $this->status->setRawInfo($FORUMS_CONFIG[$this->type][COLUMN_CONFIG][$columnIndex], trim($column->nodeValue));
                        }
                        
                        $columnIndex++;
                    }
                    
                    if($foundTopicTitleOnPage) {
                        break;
                    }
                }
            }
            
            if(!$foundTopicTitleOnPage) {
                $this->status = Status::status(Status::CODE_TOPC_NOF_FOUND_FIRST_PAGE, Status::DESCR_TOPIC_NOT_FOUND_FIRST_PAGE);
                break;
                /**
                @note. The Next-Page feature is not working properly on all forums. Disabled for now.
                
                if(! ($foundNextPage = $this->goToNextPage()) ) {
                    $this->status =  Status::status(Status::CODE_NEXT_NOT_FOUND, Status::DESCR_NEXT_NOT_FOUND, $this->currentPageNumber);
                    break;
                }
                */
            }
        }
    }    
}


$italia_mac = new Forum('Italia Mac','http://www.italiamac.it/forum/forum/56-giochi',FORUM_ITALIA_MAC,array('Eight Queens','mobisland'));
$mac_rumors = new Forum('Mac Rumors','http://forums.macrumors.com/forumdisplay.php?f=133',FORUM_MAC_RUMORS,array('Eight Queens','FREE','GAME'));

$forums_to_process = array(
    $italia_mac,
    $mac_rumors
);

foreach($forums_to_process as $forum_to_process) {
    echo '<h3>'.$forum_to_process->getName().'</h3>';
    $forum_to_process->go();
    echo var_export($forum_to_process->getStatus(),true);
    echo '<hr />';
}

?>